package com.bcmusic.music.service.impl;

import com.bcmusic.music.dao.MusicEmotionMapper;
import com.bcmusic.music.pojo.MusicEmotion;
import com.bcmusic.music.service.MusicEmotionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:MusicEmotion业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class MusicEmotionServiceImpl implements MusicEmotionService {

    @Autowired
    private MusicEmotionMapper musicEmotionMapper;


    /**
     * MusicEmotion条件+分页查询
     * @param musicEmotion 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<MusicEmotion> findPage(MusicEmotion musicEmotion, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(musicEmotion);
        //执行搜索
        return new PageInfo<MusicEmotion>(musicEmotionMapper.selectByExample(example));
    }

    /**
     * MusicEmotion分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<MusicEmotion> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<MusicEmotion>(musicEmotionMapper.selectAll());
    }

    /**
     * MusicEmotion条件查询
     * @param musicEmotion
     * @return
     */
    @Override
    public List<MusicEmotion> findList(MusicEmotion musicEmotion){
        //构建查询条件
        Example example = createExample(musicEmotion);
        //根据构建的条件查询数据
        return musicEmotionMapper.selectByExample(example);
    }


    /**
     * MusicEmotion构建查询对象
     * @param musicEmotion
     * @return
     */
    public Example createExample(MusicEmotion musicEmotion){
        Example example=new Example(MusicEmotion.class);
        Example.Criteria criteria = example.createCriteria();
        if(musicEmotion!=null){
            // 音乐情感id。
            if(!StringUtils.isEmpty(musicEmotion.getEmotionId())){
                    criteria.andEqualTo("emotionId",musicEmotion.getEmotionId());
            }
            // 音乐情感编号。
            if(!StringUtils.isEmpty(musicEmotion.getEmotionNumber())){
                    criteria.andEqualTo("emotionNumber",musicEmotion.getEmotionNumber());
            }
            // 音乐情感名称。
            if(!StringUtils.isEmpty(musicEmotion.getEmotionName())){
                    criteria.andEqualTo("emotionName",musicEmotion.getEmotionName());
            }
            // 音乐情感状态。0（false）为不可选，1（true）为可选择。
            if(!StringUtils.isEmpty(musicEmotion.getEmotionStatus())){
                    criteria.andEqualTo("emotionStatus",musicEmotion.getEmotionStatus());
            }
            // 最后修改时间。最近一次修改的时间。初始化为创建时间。
            if(!StringUtils.isEmpty(musicEmotion.getModifyTime())){
                    criteria.andEqualTo("modifyTime",musicEmotion.getModifyTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        musicEmotionMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改MusicEmotion
     * @param musicEmotion
     */
    @Override
    public void update(MusicEmotion musicEmotion){
        musicEmotionMapper.updateByPrimaryKey(musicEmotion);
    }

    /**
     * 增加MusicEmotion
     * @param musicEmotion
     */
    @Override
    public void add(MusicEmotion musicEmotion){
        musicEmotionMapper.insert(musicEmotion);
    }

    /**
     * 根据ID查询MusicEmotion
     * @param id
     * @return
     */
    @Override
    public MusicEmotion findById(Long id){
        return  musicEmotionMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询MusicEmotion全部数据
     * @return
     */
    @Override
    public List<MusicEmotion> findAll() {
        return musicEmotionMapper.selectAll();
    }
}
